package com.lsx143.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsx143.realtime.app.BaseAppV2;
import com.lsx143.realtime.bean.OrderWide;
import com.lsx143.realtime.bean.PaymentInfo;
import com.lsx143.realtime.bean.PaymentWide;
import com.lsx143.realtime.common.Constants;
import com.lsx143.realtime.util.KafkaUtil;
import com.lsx143.realtime.util.MyUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Map;

public class DWMPaymentWideApp extends BaseAppV2 {
    public static void main(String[] args) {
        new DWMPaymentWideApp().init(
                20003,
                "DWMPaymentWideApp",
                1,
                "DWMPaymentWideApp",
                Constants.TOPIC_DWM_ORDER_WIDE,
                Constants.TOPIC_DWD_PAYMENT_INFO
        );
    }

    @Override
    protected void run(StreamExecutionEnvironment env, Map<String, DataStreamSource<String>> srcStreams) {
        //支付信息
        KeyedStream<PaymentInfo, Long> keyedPaymentInfoStream = srcStreams
                .get(Constants.TOPIC_DWD_PAYMENT_INFO)
                .map(info -> JSONObject.parseObject(info, PaymentInfo.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((info, ts) -> MyUtil.toTs(info.getCreate_time()))
                )
                .keyBy(PaymentInfo::getOrder_id);
        //订单宽表
        KeyedStream<OrderWide, Long> keyedOrderWideStream = srcStreams
                .get(Constants.TOPIC_DWM_ORDER_WIDE)
                .map(order -> JSONObject.parseObject(order, OrderWide.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((order, ts) -> MyUtil.toTs(order.getCreate_time()))
                )
                .keyBy(OrderWide::getOrder_id);

        //连表输出宽表
        keyedPaymentInfoStream
                .intervalJoin(keyedOrderWideStream)
                .between(Time.minutes(-45), Time.seconds(10))
                .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                    @Override
                    public void processElement(PaymentInfo paymentInfo,
                                               OrderWide orderWide,
                                               Context ctx,
                                               Collector<PaymentWide> out) {
                        out.collect(new PaymentWide(paymentInfo, orderWide));
                    }
                })
                //转换写到kafka
                .map(JSON::toJSONString)
                .addSink(KafkaUtil.getKafkaSink(Constants.TOPIC_DWM_PAYMENT_WIDE));
    }
}
